<template>
  <div>
      <Modal v-model="modal" :title="title" >
        <Form ref="form1" :model="obj" :rules="ruleValidate" :label-width="80" >
            <FormItem label="用户名" prop="userCode">
                <Input v-model="obj.userCode" placeholder=""></Input>
            </FormItem>
            <FormItem label="昵称" prop="userName">
                <Input v-model="obj.userName" placeholder=""></Input>
            </FormItem>
            <FormItem label="密码" prop="userPwd" v-if="edit">
              <Input v-model="obj.userPwd" placeholder="" type="password"></Input>
            </FormItem>
            <FormItem label="用户类型" prop="userType">
              <Select v-model="obj.userType" >
                <Option value="0">普通用户</Option>
                <Option value="1">财务用户</Option>
            </Select>
            </FormItem>
            <!-- <FormItem prop="state" label="状态">
              <RadioGroup v-model="obj.state">
                <Radio label="1">
                    <span>开启</span>
                </Radio>
                <Radio label="0">
                    <span>关闭</span>
                </Radio>
              </RadioGroup>
            </FormItem> -->
        </Form>
        <div slot="footer">
            <Button @click="cancel()">关闭</Button>
            <Button type="primary" @click="submit('addForm')" >{{title}}</Button>
        </div>
      </Modal>
  </div>
</template>
<script>
export default {
  data (){
      return {
          modal:false,edit:false,
          obj:{},
          title:'',
          ruleValidate:{
              userCode:[
                  { required: true, message: '用户名不能为空', trigger: 'blur' }
              ],userName:[
                  { required: true, message: '昵称不能为空', trigger: 'blur' }
              ],
              userPwd:[
                 { required: true, message: '密码不能为空', trigger: 'blur' }
              ],userType:[
                 { required: true, message: '请选择用户类型', trigger: 'change' }
              ]
          }
      }
  },methods:{
      showdata(params){

          this.obj=Object.assign({},params);
          if(params.userName){
            this.title='修改'
          }else{
            this.title='添加'
            this.edit=true;
          }
          this.modal=true;
          this.$refs['form1'].resetFields();
      },

     
      submit(){
          
          if(this.handleSubmit()){
              var url='/user/add';
              if(this.title=='修改'){
                url='/user/update'
              }
              this.axios.post(url, this.obj).then((res) => {
                console.log(res);
                if(res.errorCode==0){
                  this.modal=false;
                  this.$emit('refresh');
                  this.$Message.success(this.title+'成功!')
                }
              });
              
          }
      },
      cancel(){
          this.modal=false;
      },
      handleSubmit () {
            var a=true;
            this.$refs['form1'].validate((valid) => {
                if (valid) {
                    //this.$Message.success('Success!');
                } else {
                    //this.$Message.error('Fail!');
                    a=false;
                }
            })
            return a;
      },
  },created(){
      
      this.$bus.on('bus_useredit', this.showdata);
  },beforeDestroy(){
      this.$bus.off('bus_useredit',this.showdata)
  }
}
</script>
